package com.ruoyi.web.controller.storeUser;

import java.math.BigDecimal;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jiuyou.storeController.store.StoreUserTController;
import com.ruoyi.clerkBalance.domain.ClerkBalance;
import com.ruoyi.clerkBalance.service.IClerkBalanceService;
import com.ruoyi.clerkBalanceRecord.domain.ClerkBalanceRecord;
import com.ruoyi.clerkBalanceRecord.service.IClerkBalanceRecordService;
import com.ruoyi.clerkBalanceRecord.service.impl.ClerkBalanceRecordServiceImpl;
import com.ruoyi.common.core.domain.entity.StoreUser;
import com.ruoyi.common.utils.Arith;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.staff.domain.Staff;
import com.ruoyi.staff.service.IStaffService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.storeUser.service.IStoreUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 门店用户Controller
 *
 * @author ruoyi
 * @date 2021-11-17
 */
@RestController
@RequestMapping("/storeUser/storeUser")
public class StoreUserController extends BaseController
{
    @Autowired
    private IStoreUserService storeUserService;
    @Autowired
    private  IStaffService staffService;
    @Autowired
    private StoreUserTController storeUserTController;

    @Autowired
    private IClerkBalanceService clerkBalanceService;
    @Autowired
    private IClerkBalanceRecordService clerkBalanceRecordService;

    /**
     * 查询门店用户列表
     */
    @PreAuthorize("@ss.hasPermi('storeUser:storeUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(StoreUser storeUser)
    {
        startPage();
        List<StoreUser> list = storeUserService.selectStoreUserList(storeUser);
        return getDataTable(list);
    }

    /**
     * 导出门店用户列表
     */
    @PreAuthorize("@ss.hasPermi('storeUser:storeUser:export')")
    @Log(title = "门店用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StoreUser storeUser)
    {
        List<StoreUser> list = storeUserService.selectStoreUserList(storeUser);
        ExcelUtil<StoreUser> util = new ExcelUtil<StoreUser>(StoreUser.class);
        return util.exportExcel(list, "门店用户数据");
    }

    /**
     * 获取门店用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('storeUser:storeUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(storeUserService.selectStoreUserById(id));
    }

    /**
     * 新增门店用户
     */
    @PreAuthorize("@ss.hasPermi('storeUser:storeUser:add')")
    @Log(title = "门店用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StoreUser storeUser)
    {
        return toAjax(storeUserService.insertStoreUser(storeUser));
    }

    /**
     * 修改门店用户
     */
    @PreAuthorize("@ss.hasPermi('storeUser:storeUser:edit')")
    @Log(title = "门店用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StoreUser storeUser)
    {
        storeUser.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(storeUserService.updateStoreUser(storeUser));
    }

    /**
     * 删除门店用户
     */
    @PreAuthorize("@ss.hasPermi('storeUser:storeUser:remove')")
    @Log(title = "门店用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(storeUserService.logicDeleteStoreUser(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId()));
    }

    /**
     * 新增店员列表 可查看店员信息和店员分销数据
     * 店员信息
     */

    @PreAuthorize("@ss.hasPermi('storeUser:storeUser:getStaffList')")
    @Log(title = "门店用户")
    @GetMapping("/getStaffIfoList")
    public TableDataInfo getStaffList()
    {
        Staff staffParam=new Staff();
        startPage();
        List<Staff> result = staffService.selectStaffList(staffParam);
        ClerkBalanceRecord clerkBalanceRecord = new ClerkBalanceRecord();
        clerkBalanceRecord.setBusType("div");
        for(Staff staff1:result)
        {
            staff1.setMasterList(staffService.selectMasterListById(staff1.getId()));
            staff1.setDiscipleList(staffService.selectDiscipleListById(staff1.getId()));
            staff1.setSubordinateList(staffService.selectSubordinateListById(staff1.getId()));
            ClerkBalance clerkBalance = clerkBalanceService.selectClerkBalanceByStoreUserId(staff1.getId());
            staff1.setTotalAmount(clerkBalance.getCommission());
            clerkBalanceRecord.setStoreUserId(staff1.getId());
            List<ClerkBalanceRecord> ClerkBalanceRecordList = clerkBalanceRecordService.selectClerkBalanceRecordList(clerkBalanceRecord);
            staff1.setOrderCount(ClerkBalanceRecordList.size());
        }
        TableDataInfo tableDataInfo =getDataTable(result);
        return tableDataInfo;
    }

    /**
     * 新增店员列表 可查看店员信息和店员分销数据
     * 分销明细
     */
    @PreAuthorize("@ss.hasPermi('storeUser:storeUser:getStaffClerkBalanceRecord')")
    @Log(title = "门店用户")
    @GetMapping("/getStaffClerkBalanceRecord/{staffId}")
    public AjaxResult getStaffClerkBalanceRecord(@PathVariable Long staffId)
    {
        Long storeUserId = staffId;
        ClerkBalanceRecord clerkBalanceRecord = new ClerkBalanceRecord();
        clerkBalanceRecord.setStoreUserId(storeUserId);
        clerkBalanceRecord.setBusType(DicConstants.BUSTYPE_BALANCE_DIV.getCode());
        startPage();
        List<ClerkBalanceRecord> list = clerkBalanceRecordService.selectClerkBalanceRecordList(clerkBalanceRecord);
        TableDataInfo tableDataInfo= getDataTable(list);
        JSONArray jsonArray=JSONArray.parseArray(JSONArray.toJSONString(list));
        for (Object o : jsonArray) {
            JSONObject jso=(JSONObject)o;
            jso.put("money", Arith.numTostr(jso.getBigDecimal("money")));
            jso.put("createTime", DateUtils.dateTimeYMDHSM(jso.getDate("createTime")));
        }
        return AjaxResult.success(tableDataInfo);
    }






}
